package com.apexsoft.monitor.front.console.app.service;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.monitor.front.console.app.dao.AppDao;
import com.apexsoft.monitor.front.console.app.om.App;
import com.apexsoft.monitor.front.console.pageModel.Papp;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service("appService")
public class AppService {

	private static final Logger log = LogManager.getRootLogger();
	
	@Autowired
	@Qualifier("appDao")
	private AppDao appDao = null;

	public JSONObject listByPage(Pagination page, Papp pageModel) throws Exception {

		EntityWrapper<App> entityWrapper = new EntityWrapper<>();
		if (StringUtils.isNotBlank(pageModel.getAppCode())) {
			entityWrapper.and("app_code={0}", pageModel.getAppCode());
		}
		if (StringUtils.isNotBlank(pageModel.getAppName())) {
			entityWrapper.like("app_name", pageModel.getAppName());
		}
		if (pageModel.getAppStatus()!=null){
			entityWrapper.and("app_status={0}",pageModel.getAppStatus());
		}
		List<App> list = appDao.selectPage(page, entityWrapper);
		JSONObject commonListResult = Result.commonListResult(list);
		commonListResult.put(Result.TOTAL,page.getTotal());
		return commonListResult;
	}
	public JSONObject getAllApp(String status) throws Exception {
		return Result.commonListResult(getAllAppForList(status));
	}

    public List<App> getAllAppForList(String status) throws Exception{
        EntityWrapper<App> entityWrapper = new EntityWrapper<>();
        if(StringUtils.isNotBlank(status)){
            entityWrapper.and("app_status={0}",status);
        }
        List<App> list = appDao.selectList(entityWrapper);
        return list;
    }


	public JSONObject add(HttpServletRequest request) {
		
		JSONObject result = new JSONObject();
		String appName = request.getParameter("appName");
		String appCode = request.getParameter("appCode");
		String appDesc = request.getParameter("appDesc");
		String appStatus = request.getParameter("appStatus");

		if (StringUtils.isNotBlank(appName)
				&& StringUtils.isNotBlank(appCode)
				&& StringUtils.isNotBlank(appStatus)
				) {
			
			App app = new App();
			app.setAppCode(appCode);
			app.setAppName(appName);
			app.setAppDesc(appDesc);
			app.setAppStatus(StringUtils.isNumeric(appStatus)?Integer.parseInt(appStatus):0);
			try {
				App tempApp = appDao.selectByAppCode(appCode);
				if (tempApp!=null){
					result.put(Result.CODE, Result.EC_1000);
					result.put(Result.NOTE, "应用代码["+appCode+"]已存在");
					return result;
				}
				long insertRst = appDao.insert(app);
				if (insertRst> Result.EC_1000) {
					result.put(Result.CODE, Result.SC_0001);
					result.put(Result.NOTE, "新增应用成功");
				}
			} catch (Exception e) {
				log.error("新增应用失败："+e.getMessage(), e);
				result.put(Result.CODE, Result.EC_1000);
				result.put(Result.NOTE, "新增应用失败");
			}
			
		}else {
			result.put(Result.CODE, Result.EC_1000);
			result.put(Result.NOTE, "参数不正确");
		}
		return result;
	}
	
	public JSONObject update(HttpServletRequest request) {
		
		JSONObject result = new JSONObject();
		String id = request.getParameter("id");
		String appName = request.getParameter("appName");
		String appCode = request.getParameter("appCode");
		String appDesc = request.getParameter("appDesc");
		String appStatus = request.getParameter("appStatus");
		if (StringUtils.isNotBlank(appName)
				&& StringUtils.isNotBlank(appCode)
				&& StringUtils.isNotBlank(appStatus)
				) {
			
			App app = new App();
			app.setId(Integer.parseInt(id));
			app.setAppCode(appCode);
			app.setAppName(appName);
			app.setAppDesc(appDesc);
			app.setAppStatus(StringUtils.isNumeric(appStatus)?Integer.parseInt(appStatus):0);
			try {
				int uptRst = appDao.updateSelectiveById(app);
				if (uptRst> Result.EC_1000) {
					result.put(Result.CODE, Result.SC_0001);
					result.put(Result.NOTE, "修改应用成功");
				}
			} catch (Exception e) {
				log.error("修改应用失败："+e.getMessage(), e);
				result.put(Result.CODE, Result.EC_1000);
				result.put(Result.NOTE, "修改应用失败");
			}
			
		}else {
			result.put(Result.CODE, Result.EC_1000);
			result.put(Result.NOTE, "参数不正确");
		}
		return result;
	}
	
	public JSONObject delete(String id) {
		
		JSONObject result = new JSONObject();
		if (StringUtils.isNotBlank(id)) {
			
			try {
				int delRst = appDao.deleteById(id);
				if (delRst> Result.EC_1000) {
					result.put(Result.CODE, Result.SC_0001);
					result.put(Result.NOTE, "删除应用成功");
				}
			} catch (Exception e) {
				log.error("删除应用失败："+e.getMessage(), e);
				result.put(Result.CODE, Result.EC_1000);
				result.put(Result.NOTE, "删除应用失败");
			}
				
			
		}else {
			result.put(Result.CODE, Result.EC_1000);
			result.put(Result.NOTE, "参数不正确");
		}
		return result;
	}
	
	public App getById(int id) {
		return appDao.selectById(id);
	}
	public App getByAppCode(String code) {
		return appDao.selectByAppCode(code);
	}

}
